package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Api(tags = {"部门接口"})
@RestController
@RequestMapping("/dept")
public class DepartmentController {
    //注入service层
    @Autowired
    private IDepartmentService service;

    @GetMapping("/treeList")
    private AjaxResult treeList(){
        //1.查询所有
        List<Department> list = service.treeList();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }

    /**
     * 查询所有的方法
     */
    @GetMapping("/findAll")
    private AjaxResult findAll(){
        //1.查询所有
        List<Department> list = service.findAll();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }

    /**
     * 根据id查询的方法
     */
    @GetMapping("/findOne/{id}")
    private AjaxResult findOne(@PathVariable("id") Long id){
        return AjaxResult.createSuccess(service.findOne(id));
    }

    /**
     * 新增或者修改  没有id就是新增  没有id就是修改
     */
    @PostMapping
    @ApiOperation(value = "新增/修改部门", notes = "帅兔子")
    public AjaxResult saveOrUpdate(@ApiParam(value = "新增/修改部门", required = true) @RequestBody Department department){
        //调用service层
        try {
            service.saveOrUpdate(department);
            return AjaxResult.createSuccess();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.createSuccess("系统异常,请联系最帅的管理员~~~");
        }
    }

    /**
     * 根据id删除方法
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@ApiParam(value = "删除部门id参数", required = true) @PathVariable("id") Long id){
        //调用删除方法
        try {
            service.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.createSuccess("系统异常,请联系最帅的管理员~~~");
        }
    }

    /**
     * 批量删除的方法
     */
    @PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody DepartmentQuery query){
//        try {
//            service.batchDelete(query.getIds());
//            return AjaxResult.createSuccess();
//        } catch (Exception e){
//            e.printStackTrace();
//            return AjaxResult.createSuccess("系统异常,请联系最帅的管理员~~~");
//        }
        //调用删除方法
        //还有刚刚你用query调用方法了,你没有用业务层接口调用,还有哪里有报错
        service.batchDelete(query.getIds());//这里方法里面写的是id数组,你要放,只能在query拿,getids
        return AjaxResult.createSuccess();
    }

    /**
     * 分页查询
     */
//    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    @PostMapping("/findPage")   //简写
    private PageList<Department> findPage(@RequestBody DepartmentQuery query){
        //调用业务层
        PageList<Department> page = service.findPage(query);
        System.out.println(page);
        return page;
    }

    /**
     * 查询所有的上级部门们
     */
    @PostMapping("/findParentDepts")
    private AjaxResult findParentDepts(){
        //调用业务层
        List<Department> parentDepts = service.findParentDepts();
        return AjaxResult.createSuccess(parentDepts);
        //return AjaxResult.createSuccess(departmentService.findParentDepts());
    }

}